package tree;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:墨琦
 * Data:2023-04-16
 * Time:14:48
 */
public class BinaryTree {
       static class TreeNode{
           int val;
           TreeNode left;
           TreeNode right;
           TreeNode()
           {}
           TreeNode(int num)
           {
               this.val=num;
           }
       }

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        return A;
    }
    TreeNode root=createTree();
    //前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) return;
        System.out.print(root.val+" ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //计算结点数目
    public static int num=0;
    public int countCode1(TreeNode root)
    {
        if(root == null) return 0;
        num++;
        countCode1(root.left);
        countCode1(root.right);
        return num;
    }
    public int countCode2(TreeNode root)
    {
        return countCode2(root.left)+countCode2(root.right)+1;
    }
    //叶子结点
    public static int leafSize = 0;
    public int getLeafNodeCount(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);
        return leafSize;
    }

    public int getLeafNodeCount2(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);
    }

}


